﻿namespace MicroCloud.Extensions
{
    #region "实体接口扩展方法"
    /// <summary>
    /// 实体接口扩展方法
    /// </summary>
    public static class EntityExtensions
    {
        #region "方法"
        #region "检测指定类型是否为 <see cref="IEntity{TKey}"/> 实体类型"
        /// <summary>
        /// 检测指定类型是否为 <see cref="IEntity{TKey}"/> 实体类型
        /// </summary>
        /// <param name="type">要判断的类型</param>
        /// <returns></returns>
        public static bool IsEntityType(this Type type)
        {
            Check.NotNull(type, nameof(type));
            return typeof(IEntity<>).IsGenericAssignableFrom(type) && !type.IsAbstract && !type.IsInterface;
        }
        #endregion
        #region "判断指定实体是否已过期"
        /// <summary>
        /// 判断指定实体是否已过期
        /// </summary>
        /// <param name="entity">要检测的实体</param>
        /// <returns></returns>
        public static bool IsExpired(this IExpirable entity)
        {
            Check.NotNull(entity, nameof(entity));
            DateTime now = DateTime.Now;
            return entity.BeginTime != null && entity.BeginTime.Value > now || entity.EndTime != null && entity.EndTime.Value < now;
        }
        #endregion
        #region "检测并执行 <see cref="ICreatedTime"/> 接口的逻辑"
        /// <summary>
        /// 检测并执行 <see cref="ICreatedTime"/> 接口的逻辑
        /// </summary>
        /// <typeparam name="TEntity">实体对象类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <returns>实体对象</returns>
        public static TEntity CheckICreatedTime<TEntity>(this TEntity entity)
            where TEntity : IEntityBase
        {
            if (entity is not ICreatedTime)
            {
                return entity;
            }
            ICreatedTime entity1 = (ICreatedTime)entity;
            entity1.CreatedTime = DateTime.Now;

            return (TEntity)entity1;
        }
        #endregion
        #region "检测并执行 <see cref="ICreationAudited{TUserKey}"/> 接口的处理"
        /// <summary>
        /// 检测并执行 <see cref="ICreationAudited{TUserKey}"/> 接口的处理
        /// </summary>
        /// <typeparam name="TEntity">实体对象类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <param name="user">用户身份基本功能</param>
        /// <returns>实体对象</returns>
        public static TEntity CheckICreationAudited<TEntity>(this TEntity entity, IPrincipal user)
            where TEntity : IEntityBase
        {
            if (!entity.GetType().IsBaseOn(typeof(ICreationAudited<>)) && !entity.GetType().IsBaseOn(typeof(ICreationAudited)))
            {
                return entity;
            }

            var authenticated = user?.Identity?.IsAuthenticated == true;
            #region "实体引用属性类型设定"
            var creatorIdType = entity.GetType().GetProperty("CreatorId").PropertyType.GetNonNullableType();
            if (creatorIdType == typeof(int))
            {
                ICreationAudited<int> entity1 = (ICreationAudited<int>)entity;
                entity1.CreatorId = authenticated ? user.Identity.GetUserId<int>() : null;
                entity1.CreatedTime = DateTime.Now;

                entity = (TEntity)entity1;
            }
            else if (creatorIdType == typeof(Guid))
            {
                ICreationAudited<Guid> entity1 = (ICreationAudited<Guid>)entity;
                entity1.CreatorId = authenticated ? user.Identity.GetUserId<Guid>() : null;
                entity1.CreatedTime = DateTime.Now;

                entity = (TEntity)entity1;
            }
            else if (creatorIdType == typeof(long))
            {
                ICreationAudited<long> entity1 = (ICreationAudited<long>)entity;
                entity1.CreatorId = authenticated ? user.Identity.GetUserId<long>() : null;
                entity1.CreatedTime = DateTime.Now;

                entity = (TEntity)entity1;
            }
            else
            {
                ICreationAudited entity1 = (ICreationAudited)entity;
                entity1.CreatorId = authenticated ? user.Identity.GetUserId() : "";
                entity1.CreatedTime = DateTime.Now;

                entity = (TEntity)entity1;
            }
            #endregion

            #region "根据用户ID类型设定逻辑"
            //string userIdTypeName = user?.Identity.GetClaimValueFirstOrDefault(SettingKeyConstants.UserIdTypeName);
            //if (userIdTypeName != null)
            //{
            //    if (userIdTypeName == typeof(int).FullName)
            //    {
            //        ICreationAudited<int> entity1 = (ICreationAudited<int>)entity;
            //        entity1.CreatorId = authenticated ? user.Identity.GetUserId<int>() : null;
            //        entity1.CreatedTime = DateTime.Now;

            //        entity = (TEntity)entity1;
            //    }
            //    else if (userIdTypeName == typeof(Guid).FullName)
            //    {
            //        ICreationAudited<Guid> entity1 = (ICreationAudited<Guid>)entity;
            //        entity1.CreatorId = authenticated ? user.Identity.GetUserId<Guid>() : null;
            //        entity1.CreatedTime = DateTime.Now;

            //        entity = (TEntity)entity1;
            //    }
            //    else if (userIdTypeName == typeof(long).FullName)
            //    {
            //        ICreationAudited<long> entity1 = (ICreationAudited<long>)entity;
            //        entity1.CreatorId = authenticated ? user.Identity.GetUserId<long>() : null;
            //        entity1.CreatedTime = DateTime.Now;

            //        entity = (TEntity)entity1;
            //    }
            //    else
            //    {
            //        ICreationAudited entity1 = (ICreationAudited)entity;
            //        entity1.CreatorId = authenticated ? user.Identity.GetUserId() : "";
            //        entity1.CreatedTime = DateTime.Now;

            //        entity = (TEntity)entity1;
            //    }
            //}
            #endregion

            return entity;
        }
        #endregion
        #region "检测并执行 <see cref="IUpdatedTime"/> 接口的逻辑"
        /// <summary>
        /// 检测并执行 <see cref="IUpdatedTime"/> 接口的逻辑
        /// </summary>
        /// <typeparam name="TEntity">实体对象类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <returns>实体对象</returns>
        public static TEntity CheckIUpdatedTime<TEntity>(this TEntity entity)
            where TEntity : IEntityBase
        {
            if (entity is not IUpdatedTime)
            {
                return entity;
            }
            IUpdatedTime entity1 = (IUpdatedTime)entity;
            entity1.UpdatedTime = DateTime.Now;

            return (TEntity)entity1;
        }
        #endregion
        #region "检测并执行 <see cref="IUpdateAudited{TUserKey}"/> 接口的处理"
        /// <summary>
        /// 检测并执行 <see cref="IUpdateAudited{TUserKey}"/> 接口的处理
        /// </summary>
        /// <typeparam name="TEntity">实体对象类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <param name="user">用户身份基本功能</param>
        /// <returns>实体对象</returns>
        public static TEntity CheckIUpdateAudited<TEntity>(this TEntity entity, IPrincipal user)
            where TEntity : IEntityBase
        {
            if (!entity.GetType().IsBaseOn(typeof(IUpdateAudited<>)) && !entity.GetType().IsBaseOn(typeof(IUpdateAudited)))
            {
                return entity;
            }

            var authenticated = user?.Identity?.IsAuthenticated == true;
            #region "实体引用属性类型设定"
            var updaterIdType = entity.GetType().GetProperty("UpdaterId").PropertyType.GetNonNullableType();
            if (updaterIdType == typeof(int))
            {
                var entity1 = (IUpdateAudited<int>)entity;
                entity1.UpdaterId = authenticated ? user.Identity.GetUserId<int>() : null;
                entity1.UpdatedTime = DateTime.Now;

                entity = (TEntity)entity1;
            }
            else if (updaterIdType == typeof(Guid))
            {
                var entity1 = (IUpdateAudited<Guid>)entity;
                entity1.UpdaterId = authenticated ? user.Identity.GetUserId<Guid>() : null;
                entity1.UpdatedTime = DateTime.Now;

                entity = (TEntity)entity1;
            }
            else if (updaterIdType == typeof(long))
            {
                var entity1 = (IUpdateAudited<long>)entity;
                entity1.UpdaterId = authenticated ? user.Identity.GetUserId<long>() : null;
                entity1.UpdatedTime = DateTime.Now;

                entity = (TEntity)entity1;
            }
            else
            {
                var entity1 = (IUpdateAudited)entity;
                entity1.UpdaterId = authenticated ? user.Identity.GetUserId<string>() : "";
                entity1.UpdatedTime = DateTime.Now;

                entity = (TEntity)entity1;
            }
            #endregion

            #region "根据用户ID类型设定逻辑"
            //string userIdTypeName = user?.Identity.GetClaimValueFirstOrDefault(SettingKeyConstants.UserIdTypeName);
            //if (userIdTypeName != null)
            //{
            //    if (userIdTypeName == typeof(int).FullName)
            //    {
            //        var entity1 = (IUpdateAudited<int>)entity;
            //        entity1.UpdaterId = authenticated ? user.Identity.GetUserId<int>() : null;
            //        entity1.UpdatedTime = DateTime.Now;

            //        entity = (TEntity)entity1;
            //    }
            //    else if (userIdTypeName == typeof(Guid).FullName)
            //    {
            //        var entity1 = (IUpdateAudited<Guid>)entity;
            //        entity1.UpdaterId = authenticated ? user.Identity.GetUserId<Guid>() : null;
            //        entity1.UpdatedTime = DateTime.Now;

            //        entity = (TEntity)entity1;
            //    }
            //    else if (userIdTypeName == typeof(long).FullName)
            //    {
            //        var entity1 = (IUpdateAudited<long>)entity;
            //        entity1.UpdaterId = authenticated ? user.Identity.GetUserId<long>() : null;
            //        entity1.UpdatedTime = DateTime.Now;

            //        entity = (TEntity)entity1;
            //    }
            //    else
            //    {
            //        var entity1 = (IUpdateAudited)entity;
            //        entity1.UpdaterId = authenticated ? user.Identity.GetUserId<string>() : "";
            //        entity1.UpdatedTime = DateTime.Now;

            //        entity = (TEntity)entity1;
            //    }
            //}
            #endregion

            return entity;
        }
        #endregion
        #region "检测并执行 <see cref="IOrganizationAudited{TOrganizationKey}"/> 接口的逻辑"
        /// <summary>
        /// 检测并执行 <see cref="IOrganizationAudited{TOrganizationKey}"/> 接口的逻辑
        /// </summary>
        /// <typeparam name="TEntity">数据实体类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <param name="provider">服务提供者</param>
        /// <returns>实体对象</returns>
        public static TEntity CheckIOrganizationAudited<TEntity>(this TEntity entity, IServiceProvider provider)
            where TEntity : IEntityBase
        {
            if (!entity.GetType().IsBaseOn(typeof(IOrganizationAudited<>)) && !entity.GetType().IsBaseOn(typeof(IOrganizationAudited)))
            {
                return entity;
            }
            var principal = provider.GetService<IPrincipal>();
            if (principal?.Identity?.IsAuthenticated != true)
            {
                return entity;
            }
            var organizationId = principal.Identity.GetCurrentOrganizationId();
            organizationId ??= "";
            var organizationProvider = provider.GetService<IOrganizationProvider>();

            #region "实体引用属性类型设定"
            var organizationIdType = entity.GetType().GetProperty("OrganizationId").PropertyType.GetNonNullableType();
            if (organizationIdType == typeof(int))
            {
                var entity1 = (IOrganizationAudited<int>)entity;
                if (entity1.OrganizationId == default)
                {
                    entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<int>();
                }
                //验证
                CheckOrganization(entity1.OrganizationId.ToString(), organizationProvider);

                entity = (TEntity)entity1;
            }
            else if (organizationIdType == typeof(Guid))
            {
                var entity1 = (IOrganizationAudited<Guid>)entity;
                if (entity1.OrganizationId == default)
                {
                    entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<Guid>();
                }
                //验证
                CheckOrganization(entity1.OrganizationId.ToString(), organizationProvider);

                entity = (TEntity)entity1;
            }
            else if (organizationIdType == typeof(long))
            {
                var entity1 = (IOrganizationAudited<long>)entity;
                if (entity1.OrganizationId == default)
                {
                    entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<long>();
                }
                //验证
                CheckOrganization(entity1.OrganizationId.ToString(), organizationProvider);

                entity = (TEntity)entity1;
            }
            else
            {
                var entity1 = (IOrganizationAudited)entity;
                if (entity1.OrganizationId == default)
                {
                    entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId;
                }
                //验证
                CheckOrganization(entity1.OrganizationId, organizationProvider);

                entity = (TEntity)entity1;
            }
            #endregion

            #region "根据组织机构ID类型设定逻辑"
            //string orgIdTypeName = principal?.Identity.GetClaimValueFirstOrDefault(SettingKeyConstants.OrgIdTypeName);
            //if (orgIdTypeName != null)
            //{
            //    if (orgIdTypeName == typeof(int).FullName)
            //    {
            //        var entity1 = (IOrganizationAudited<int>)entity;
            //        if (entity1.OrganizationId == default)
            //        {
            //            entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<int>();
            //        }
            //        //验证
            //        CheckOrganization(entity1.OrganizationId.ToString(), organizationProvider);

            //        entity = (TEntity)entity1;
            //    }
            //    else if (orgIdTypeName == typeof(Guid).FullName)
            //    {
            //        var entity1 = (IOrganizationAudited<Guid>)entity;
            //        if (entity1.OrganizationId == default)
            //        {
            //            entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<Guid>();
            //        }
            //        //验证
            //        CheckOrganization(entity1.OrganizationId.ToString(), organizationProvider);

            //        entity = (TEntity)entity1;
            //    }
            //    else if (orgIdTypeName == typeof(long).FullName)
            //    {
            //        var entity1 = (IOrganizationAudited<long>)entity;
            //        if (entity1.OrganizationId == default)
            //        {
            //            entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<long>();
            //        }
            //        //验证
            //        CheckOrganization(entity1.OrganizationId.ToString(), organizationProvider);

            //        entity = (TEntity)entity1;
            //    }
            //    else
            //    {
            //        var entity1 = (IOrganizationAudited)entity;
            //        if (entity1.OrganizationId == default)
            //        {
            //            entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId;
            //        }
            //        //验证
            //        CheckOrganization(entity1.OrganizationId, organizationProvider);

            //        entity = (TEntity)entity1;
            //    }
            //}
            #endregion

            return entity;
        }
        #endregion
        #region "检测并执行 <see cref="IDepartmentAudited{TOrganizationKey}"/> 接口的逻辑"
        /// <summary>
        /// 检测并执行 <see cref="IDepartmentAudited{TOrganizationKey}"/> 接口的逻辑
        /// </summary>
        /// <typeparam name="TEntity">数据实体类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <param name="provider">服务提供者</param>
        /// <returns>实体对象</returns>
        public static TEntity CheckIDepartmentAudited<TEntity>(this TEntity entity, IServiceProvider provider)
            where TEntity : IEntityBase
        {
            if (!entity.GetType().IsBaseOn(typeof(IDepartmentAudited<>)))
            {
                return entity;
            }
            var principal = provider.GetService<IPrincipal>();
            if (principal?.Identity?.IsAuthenticated != true)
            {
                return entity;
            }
            var organizationId = principal.Identity.GetCurrentOrganizationId();
            organizationId ??= "";
            var departmentId = principal.Identity.GetCurrentDepartmentId();
            departmentId ??= "";
            var organizationProvider = provider.GetService<IOrganizationProvider>();

            #region "实体引用属性类型设定"
            var organizationIdType = entity.GetType().GetProperty("DepartmentId").PropertyType.GetNonNullableType();
            if (organizationIdType == typeof(int))
            {
                var entity1 = (IDepartmentAudited<int>)entity;
                if (entity1.OrganizationId == default)
                {
                    entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<int>();
                }
                if (entity1.DepartmentId == default)
                {
                    entity1.DepartmentId = departmentId.IsMissing() ? entity1.DepartmentId : departmentId.CastTo<int>();
                }
                //验证
                CheckDepartment(entity1.OrganizationId.ToString(), entity1.DepartmentId.ToString(), organizationProvider);

                entity = (TEntity)entity1;
            }
            else if (organizationIdType == typeof(Guid))
            {
                var entity1 = (IDepartmentAudited<Guid>)entity;
                if (entity1.OrganizationId == default)
                {
                    entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<Guid>();
                }
                if (entity1.DepartmentId == default)
                {
                    entity1.DepartmentId = departmentId.IsMissing() ? entity1.DepartmentId : departmentId.CastTo<Guid>();
                }
                //验证
                CheckDepartment(entity1.OrganizationId.ToString(), entity1.DepartmentId.ToString(), organizationProvider);

                entity = (TEntity)entity1;
            }
            else if (organizationIdType == typeof(long))
            {
                var entity1 = (IDepartmentAudited<long>)entity;
                if (entity1.OrganizationId == default)
                {
                    entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<long>();
                }
                if (entity1.DepartmentId == default)
                {
                    entity1.DepartmentId = departmentId.IsMissing() ? entity1.DepartmentId : departmentId.CastTo<long>();
                }
                //验证
                CheckDepartment(entity1.OrganizationId.ToString(), entity1.DepartmentId.ToString(), organizationProvider);

                entity = (TEntity)entity1;
            }
            else
            {
                var entity1 = (IDepartmentAudited)entity;
                if (entity1.OrganizationId == default)
                {
                    entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId;
                }
                if (entity1.DepartmentId == default)
                {
                    entity1.DepartmentId = departmentId.IsMissing() ? entity1.DepartmentId : departmentId;
                }
                //验证
                CheckDepartment(entity1.OrganizationId, entity1.DepartmentId, organizationProvider);

                entity = (TEntity)entity1;
            }
            #endregion

            #region "根据组织机构ID类型设定逻辑"
            //string orgIdTypeName = principal?.Identity.GetClaimValueFirstOrDefault(SettingKeyConstants.OrgIdTypeName);
            //if (orgIdTypeName != null)
            //{
            //    if (orgIdTypeName == typeof(int).FullName)
            //    {
            //        var entity1 = (IDepartmentAudited<int>)entity;
            //        if (entity1.OrganizationId == default)
            //        {
            //            entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<int>();
            //        }
            //        if (entity1.DepartmentId == default)
            //        {
            //            entity1.DepartmentId = departmentId.IsMissing() ? entity1.DepartmentId : departmentId.CastTo<int>();
            //        }
            //        //验证
            //        CheckDepartment(entity1.OrganizationId.ToString(), entity1.DepartmentId.ToString(), organizationProvider);

            //        entity = (TEntity)entity1;
            //    }
            //    else if (orgIdTypeName == typeof(Guid).FullName)
            //    {
            //        var entity1 = (IDepartmentAudited<Guid>)entity;
            //        if (entity1.OrganizationId == default)
            //        {
            //            entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<Guid>();
            //        }
            //        if (entity1.DepartmentId == default)
            //        {
            //            entity1.DepartmentId = departmentId.IsMissing() ? entity1.DepartmentId : departmentId.CastTo<Guid>();
            //        }
            //        //验证
            //        CheckDepartment(entity1.OrganizationId.ToString(), entity1.DepartmentId.ToString(), organizationProvider);

            //        entity = (TEntity)entity1;
            //    }
            //    else if (orgIdTypeName == typeof(long).FullName)
            //    {
            //        var entity1 = (IDepartmentAudited<long>)entity;
            //        if (entity1.OrganizationId == default)
            //        {
            //            entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId.CastTo<long>();
            //        }
            //        if (entity1.DepartmentId == default)
            //        {
            //            entity1.DepartmentId = departmentId.IsMissing() ? entity1.DepartmentId : departmentId.CastTo<long>();
            //        }
            //        //验证
            //        CheckDepartment(entity1.OrganizationId.ToString(), entity1.DepartmentId.ToString(), organizationProvider);

            //        entity = (TEntity)entity1;
            //    }
            //    else
            //    {
            //        var entity1 = (IDepartmentAudited)entity;
            //        if (entity1.OrganizationId == default)
            //        {
            //            entity1.OrganizationId = organizationId.IsMissing() ? entity1.OrganizationId : organizationId;
            //        }
            //        if (entity1.DepartmentId == default)
            //        {
            //            entity1.DepartmentId = departmentId.IsMissing() ? entity1.DepartmentId : departmentId;
            //        }
            //        //验证
            //        CheckDepartment(entity1.OrganizationId, entity1.DepartmentId, organizationProvider);

            //        entity = (TEntity)entity1;
            //    }
            //}
            #endregion

            return entity;
        }
        #endregion
        #region "检测并执行 <see cref="IDataVersion"/> 接口的逻辑"
        /// <summary>
        /// 检测并执行 <see cref="IDataVersion"/> 接口的逻辑
        /// </summary>
        /// <typeparam name="TEntity">数据实体类型</typeparam>
        /// <typeparam name="TKey">数据实体编号类型</typeparam>
        /// <param name="entity">数据实体</param>
        /// <param name="provider">服务提供者</param>
        /// <returns>实体对象</returns>
        public static TEntity CheckDataVersion<TEntity, TKey>(this TEntity entity, IServiceProvider provider)
            where TEntity : IEntity<TKey>
            where TKey : IEquatable<TKey>
        {
            if (entity is not IDataVersion dataVersionEntity)
            {
                return entity;
            }
            if (!entity.GetType().IsBaseOn(typeof(IEntity<>)))
            {
                return entity;
            }
            //Start:乐观并发逻辑(手动检测逻辑)
            var entityId = ((dynamic)entity).Id;
            //var entityIdType = entityId.GetType();
            long? dbDataVersion = null;
            var repository = provider.GetService<IRepository<TEntity, TKey>>();
            var dbEntity = repository.QueryAsNoTracking(o => o.Id.Equals(entity.Id)).Select(o => new { o.Id, ((IDataVersion)o).DataVersion }).FirstOrDefault();
            dbDataVersion = dbEntity?.DataVersion;
            if (dbDataVersion != null)
            {
                if (dataVersionEntity.DataVersion == default)
                {
                    throw new Exception(I18N.T("编号为 {0} 的 {1} 由于启用数据版本控制，提交修改时需要提供 {2} 值。", entityId, typeof(TEntity).GetDescription(), nameof(dataVersionEntity.DataVersion)));
                }
                if (dataVersionEntity.DataVersion < dbDataVersion.Value)
                {
                    throw new Exception(I18N.T("编号为 {0} 的 {1} 已被其他用户或终端修改，请刷新后重试。", entityId, typeof(TEntity).GetDescription()));
                }
            }
            //End:乐观并发逻辑(手动检测逻辑)
            var dataVersion = DateTime.UtcNow.ToString("yyyyMMddHHmmssfffff").CastTo<long>();
            dataVersionEntity.DataVersion = dataVersion;

            return (TEntity)dataVersionEntity;
        }
        #endregion

        #endregion


        #region "受保护的方法"
        #region "验证组织机构"
        /// <summary>
        /// 验证组织机构
        /// </summary>
        /// <param name="organizationId">组织机构编号</param>
        /// <param name="provider">组织机构信息提供者</param>
        /// <returns>组织机构DTO</returns>
        private static OrganizationDtoSimple CheckOrganization(string organizationId, IOrganizationProvider provider)
        {
            OrganizationDtoSimple organizationDto = null;
            if (!organizationId.IsMissing())
            {
                organizationDto = provider.GetOrganizationById(organizationId);
                if (organizationDto == null)
                {
                    throw new Exception(I18N.T("组织机构 {0} 不存在", organizationId));
                }
            }
            return organizationDto;
        }
        #endregion
        #region "验证部门"
        /// <summary>
        /// 验证部门
        /// </summary>
        /// <param name="organizationId">组织机构编号</param>
        /// <param name="departmentId">部门编号</param>
        /// <param name="provider">组织机构信息提供者</param>
        /// <returns>部门DTO</returns>
        private static OrganizationDtoSimple CheckDepartment(string organizationId, string departmentId, IOrganizationProvider provider)
        {
            OrganizationDtoSimple organizationDto = CheckOrganization(organizationId, provider);
            OrganizationDtoSimple departmentDto = null;
            if (!departmentId.IsMissing())
            {
                departmentDto = provider.GetOrganizationById(departmentId);
                if (departmentDto == null)
                {
                    throw new Exception(I18N.T("部门 {0} 不存在", departmentId));
                }
                if (organizationDto != null)
                {
                    if (!departmentDto.TreePathString.StartsWith(organizationDto.TreePathString))
                    {
                        throw new Exception(I18N.T("部门 {0} 不是组织 {1} 的下级机构", departmentId, organizationId));
                    }
                }
            }
            return departmentDto;
        }
        #endregion

        #endregion

    }
    #endregion

}
